﻿using System;
using System.Text;
using System.Windows.Forms;
using PhiStore.Packages.InternalPackages;
using PhiStore.Security;
using PhiStore.Storage.UI;

namespace PhiStore
{
    internal partial class ServerUI : Form
    {
        private Timer t = new Timer();
        private bool showLog = true;

        public ServerUI()
        {
            InitializeComponent();
            t.Interval = 1000;
            t.Tick += new EventHandler(UpdateLogEntry);
            t.Start();

            PhiServer.Instance.Stop();//initializing
            settings.Checked = SettingsManager.Instance.StoreSettings;
            this.portNumber.Value = settings.Checked ? 24282 : 24281;
            if (!settings.Checked)
                settings.Enabled = false;

            PhiServer.Instance.Port = (int)this.portNumber.Value;
        }

        void UpdateLogEntry(object sender, EventArgs e)
        {
            Log l = Log.Instance;
            long count = 0;

            if (showLog)
            {
                StringBuilder sb = new StringBuilder();
                while (l.Count > 0)
                {
                    sb.AppendLine(l.ReadLogEntry());
                    count++;
                }

                LogBox.Text += sb.ToString();

                if (count > 0)
                {
                    LogBox.SelectionStart = LogBox.Text.Length;
                    LogBox.ScrollToCaret();
                }
            }

            Broadcasts.Text = Log.Instance.Beeps().ToString() + " Broadcasts";

            if (count == 0)
            {
                LogStatus.Text = "Log was full " + l.FullCount.ToString() + " times";
            }
        }

        private void startStopServer_Click(object sender, EventArgs e)
        {
            if (StartStopServer.Text == "Start")
            {
                PhiServer.Instance.Start();
                if (PhiServer.Instance.Running)
                    StartStopServer.Text = "Stop";
            }
            else
            {
                PhiServer.Instance.Stop();
                StartStopServer.Text = "Start";
            }
        }

        private void ServerUI_FormClosed(object sender, FormClosedEventArgs e)
        {
            PhiServer.Instance.Stop();
        }

        private void ManageCredentials_Click(object sender, EventArgs e)
        {
            new CredentialOverviewUI().ShowDialog();
        }

        private void settings_CheckedChanged(object sender, EventArgs e)
        {
            SettingsManager.Instance.StoreSettings = settings.Checked;
        }

        private void ManageDevices_Click(object sender, EventArgs e)
        {
            new StorageOverviewUI().ShowDialog();
        }

        private void FTPTest_Click(object sender, EventArgs e)
        {
            //FtpStorage toTest = StorageManager.Instance.GetStorage<FtpStorage>();

            //toTest.Works();

            //toTest.Create("test/");
            //toTest.Rename("test/", "test2/");
            //toTest.DeleteDirectory("test/");
            //toTest.DeleteDirectory("test2/");
        }

        private void broadcastPort_ValueChanged(object sender, EventArgs e)
        {
            int newPort = (int)broadcastPort.Value;
            if (newPort == PhiServer.Instance.Port && PhiServer.Instance.Broadcast)
            {
                ToggleBroadcasting_Click(sender, e); //STOP
            }
            else
            {
                PhiServer.Instance.BroadcastPort = newPort;
            }
        }

        private void broadcastInterval_ValueChanged(object sender, EventArgs e)
        {
            PhiServer.Instance.BroadcastInterval = (int)broadcastInterval.Value;
        }

        private void ToggleBroadcasting_Click(object sender, EventArgs e)
        {
            PhiServer.Instance.Broadcast = !PhiServer.Instance.Broadcast;
            if (StartStopBC.Text == "Enable")
            {
                StartStopBC.Text = "Disable";
            }
            else
            {
                StartStopBC.Text = "Enable";
            }
        }

        private void ClearLog_Click(object sender, EventArgs e)
        {
            LogBox.Text = "";
        }

        private void portNumber_ValueChanged(object sender, EventArgs e)
        {
            int newPort = (int)portNumber.Value;
            if (newPort == PhiServer.Instance.BroadcastPort && PhiServer.Instance.Broadcast)
            {
                ToggleBroadcasting_Click(sender, e); //STOP
            }
            else
            {
                PhiServer.Instance.Port = newPort;
            }
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            PhiServer.Instance.StartConnect(remoteHost.Text, (int)remotePort.Value);
            if (PhiServer.Instance.Running)
                StartStopServer.Text = "Stop";
        }

        private void GC_Click(object sender, EventArgs e)
        {
            if (PhiServer.Instance.ObjectManagerChannel != null && !PhiServer.Instance.ObjectManagerChannel.IsPoisoned)
                PhiServer.Instance.ObjectManagerChannel.Send(new GarbageCollect());
        }

        private void ShowLog_CheckedChanged(object sender, EventArgs e)
        {
            showLog = ShowLog.Checked;
        }
    }
}
